<template>
  <div class="app-breadcrumb">
    <a-breadcrumb :routes="routes">
      <template #itemRender="{ route, routes, paths }">
        <span v-if="routes.indexOf(route) === routes.length - 1">
          {{ route.breadcrumbName }}
        </span>
        <router-link v-else :key="paths" :to="`${paths.join('/')}`">
          <home />
          {{ route.breadcrumbName }}
        </router-link>
      </template>
    </a-breadcrumb>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { Home } from '@icon-park/vue-next'
// import { useStore } from 'vuex'
// import { Breadcrumb } from '/@/store/modules/app'
interface Route {
  path: string
  breadcrumbName: string
  children?: Array<{
    path: string
    breadcrumbName: string
  }>
}

export default defineComponent({
  name: 'BreadCrumb',
  components: {
    Home,
  },

  setup() {
    // const store = useStore()
    const route = useRoute()
    const routes = ref<Route[]>([])
    const getbreadcrumbList = () => {
      const matched = route.matched.filter(
        (item) => item.meta && item.meta.title && item.meta.title !== 'dashboard',
      )
      console.log(matched)
      routes.value = []
      matched.forEach((item) => {
        routes.value.push({
          path: item.path,
          breadcrumbName: String(item.meta.title),
        })
      })
    }
    getbreadcrumbList()
    watch(
      () => route.path,
      () => {
        getbreadcrumbList()
      },
    )

    // const breadcrumbList = computed<Breadcrumb[]>(() => store.state.app.breadcrumbList)

    return {
      basePath: '/',
      routes,
    }
  },

  methods: {
    // toggleSiderBar() {
    //   alert(1)
    // }
  },
})
</script>

<style lang="scss">
.app-breadcrumb {
  display: inline-block;
  margin-left: 20px;
  .ant-breadcrumb {
    line-height: 64px;
  }
}
</style>
